package com.zenithmind.chat.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * 群组设置VO - 遵循单一职责原则
 * 只负责群组设置信息的视图展示
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "群组设置VO")
public class ChatGroupSettingVO {

    @Schema(description = "设置ID")
    private String id;

    @Schema(description = "群组ID")
    private String groupId;

    @Schema(description = "群组名称")
    private String groupName;

    @Schema(description = "群组描述")
    private String description;

    @Schema(description = "群组头像")
    private String avatar;

    @Schema(description = "群组公告")
    private String notice;

    @Schema(description = "加群验证")
    private Boolean joinVerification;

    @Schema(description = "全员禁言")
    private Boolean allMuted;

    @Schema(description = "是否公开群组")
    private Boolean isPublic;

    @Schema(description = "允许成员邀请")
    private Boolean allowMemberInvite;

    @Schema(description = "最大成员数")
    private Integer maxMembers;

    @Schema(description = "当前成员数")
    private Integer currentMembers;

    @Schema(description = "消息保留天数")
    private Integer messageRetentionDays;

    @Schema(description = "群组标签")
    private String tags;

    @Schema(description = "群组分类")
    private String category;

    @Schema(description = "地理位置")
    private String location;

    @Schema(description = "扩展设置")
    private String extraSettings;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @Schema(description = "创建者ID")
    private String creatorId;

    @Schema(description = "创建者名称")
    private String creatorName;

    @Schema(description = "最后修改者ID")
    private String lastModifierId;

    @Schema(description = "最后修改者名称")
    private String lastModifierName;

    /**
     * 获取显示的群组名称
     * 
     * @return 显示名称
     */
    public String getDisplayGroupName() {
        if (groupName != null && !groupName.trim().isEmpty()) {
            return groupName;
        }
        return "未命名群组";
    }

    /**
     * 获取显示的群组头像
     * 
     * @return 显示头像
     */
    public String getDisplayAvatar() {
        if (avatar != null && !avatar.trim().isEmpty()) {
            return avatar;
        }
        return "/images/default-group-avatar.png";
    }

    /**
     * 检查是否需要加群验证
     * 
     * @return 是否需要验证
     */
    public boolean needJoinVerification() {
        return joinVerification != null && joinVerification;
    }

    /**
     * 检查是否全员禁言
     * 
     * @return 是否全员禁言
     */
    public boolean isAllMuted() {
        return allMuted != null && allMuted;
    }

    /**
     * 检查是否为公开群组
     * 
     * @return 是否公开
     */
    public boolean isPublicGroup() {
        return isPublic != null && isPublic;
    }

    /**
     * 检查是否允许成员邀请
     * 
     * @return 是否允许成员邀请
     */
    public boolean allowMemberInvite() {
        return allowMemberInvite != null && allowMemberInvite;
    }

    /**
     * 获取群组类型名称
     * 
     * @return 群组类型名称
     */
    public String getGroupTypeName() {
        if (isPublicGroup()) {
            return "公开群组";
        }
        return "私密群组";
    }

    /**
     * 获取加群验证状态名称
     * 
     * @return 验证状态名称
     */
    public String getJoinVerificationName() {
        if (needJoinVerification()) {
            return "需要验证";
        }
        return "无需验证";
    }

    /**
     * 获取禁言状态名称
     * 
     * @return 禁言状态名称
     */
    public String getMuteStatusName() {
        if (isAllMuted()) {
            return "全员禁言";
        }
        return "正常";
    }

    /**
     * 获取成员邀请权限名称
     * 
     * @return 邀请权限名称
     */
    public String getInvitePermissionName() {
        if (allowMemberInvite()) {
            return "允许成员邀请";
        }
        return "仅管理员邀请";
    }

    /**
     * 计算成员使用率
     * 
     * @return 成员使用率（百分比）
     */
    public Double getMemberUsageRate() {
        if (maxMembers == null || maxMembers == 0 || currentMembers == null) {
            return 0.0;
        }
        return (double) currentMembers / maxMembers * 100;
    }

    /**
     * 检查是否接近成员上限
     * 
     * @return 是否接近上限（使用率超过90%）
     */
    public boolean isNearMemberLimit() {
        return getMemberUsageRate() > 90.0;
    }

    /**
     * 检查是否已满员
     * 
     * @return 是否已满员
     */
    public boolean isMemberFull() {
        if (maxMembers == null || currentMembers == null) {
            return false;
        }
        return currentMembers >= maxMembers;
    }

    /**
     * 获取剩余可加入人数
     * 
     * @return 剩余人数
     */
    public Integer getRemainingSlots() {
        if (maxMembers == null || currentMembers == null) {
            return 0;
        }
        return Math.max(0, maxMembers - currentMembers);
    }

    /**
     * 获取标签列表
     * 
     * @return 标签数组
     */
    public String[] getTagArray() {
        if (tags == null || tags.trim().isEmpty()) {
            return new String[0];
        }
        return tags.split(",");
    }

    /**
     * 检查是否有公告
     * 
     * @return 是否有公告
     */
    public boolean hasNotice() {
        return notice != null && !notice.trim().isEmpty();
    }

    /**
     * 获取简短公告（用于列表显示）
     * 
     * @param maxLength 最大长度
     * @return 简短公告
     */
    public String getShortNotice(int maxLength) {
        if (!hasNotice()) {
            return "";
        }
        if (notice.length() <= maxLength) {
            return notice;
        }
        return notice.substring(0, maxLength) + "...";
    }
}
